<template>
	<view class="content">
		<loading :loading="loading" />
		<view class="title-wrap item-wrap">
			<rich-text :nodes="data.title" class="title"></rich-text>
		</view>
		<u-row justify="between" class="type-wrap">
			<u--text :text="data.category" type="primary"></u--text>
			<u-row v-if="data.pageViews">
				<view class="" style="white-space: nowrap;margin-right: 0.2em">
					<u--text text="阅读:"></u--text>
				</view>
				<u--text :text="data.pageViews | priceFormat"></u--text>
			</u-row>
		</u-row>
		<view class="summary-wrap item-wrap">
			<rich-text :nodes="data.summary" class="summary"></rich-text>
		</view>
		<view class="tag-wrap">
			<view class="tag" v-for="(item,index) in data.tags" :key="index">
				<u-tag size="mini" :text="item" plain icon="tags"> </u-tag>
			</view>
		</view>
		<previewImage ref="previewImage" :imgs="data.images.map(item => item.value)"></previewImage>
		<view v-for="(item, index) in data.images" :key="index" class="item-wrap" @click="openView(index)">
			<u--image width="100%" :height="data.images[index].status === 0 ? '80rpx': 'auto'" class="img" :showLoading="true"
				:lazyLoad="true" 
				:src="item.value" 
				@load="()=> data.images[index].status = 1"
				mode="widthFix" v-if="!loading">
				<template v-slot:loading>
					<u-loading-icon></u-loading-icon>
				</template>
			</u--image>
		</view>
	</view>
</template>

<script>
	import {
		get
	} from "../../api/mitoPost.js"
	import previewImage from '../../components/kxj-previewImage/kxj-previewImage.vue';
	import {
		imageBaseURL
	} from "../../api/index.js";

	export default {
		components: {
			previewImage
		},
		data() {
			return {
				id: undefined,
				data: {
					"originUrl": "",
					"title": "​​​",
					"category": "​​​",
					"summary": "​​​",
					"pic": "​​​",
					"images": [],
					"tags": [],
					"pageViews": null,
					"dataFlag": null,
					"imageList": null,
					"tagList": null
				},
				loading: true
			}
		},
		onLoad(options) {
			const id = options?.id
			if (uni.$u.test.digits(id)) {
				this.id = Number(id)
				this.getData()
			}
		},
		filters: {
			priceFormat: function(value) {
				return uni.$u.priceFormat(value)
			}
		},
		methods: {
			getData() {
				this.loading = true
				get(this.id).then(data => {
					this.data = data
					this.data.images = [data.pic, ...JSON.parse(data.images || [])].map(value => ({
						value,
						status: 0
					}))
					this.data.tags = JSON.parse(data.tags)
					this.loading = false
				})
			},
			openView(index) {
				this.$refs.previewImage.open(index)
			},
			onPullDownRefresh() {
				this.getData()
				uni.stopPullDownRefresh();
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {

		padding: 20rpx 20rpx 0;

		.title-wrap {
			font-size: 1.2em;
			font-weight: bold;
		}

		.summary-wrap {
			padding: 20rpx 0;
			color: $u-main-color;
		}

		.item-wrap {
			padding-bottom: 20rpx;
		}

		.tag-wrap {
			display: flex;
			flex-wrap: wrap;
			padding-bottom: 10rpx;

			.tag {
				margin-right: 20rpx;
				margin-bottom: 20rpx;
			}
		}

	}
</style>
